<?php 

require_once '../data/connect.class.php';

class AddrOpt {
	
	var $connection;
	function AddrOpt($conn) {
		$this->connection = $conn;
	}
	function removeAddr($id) {
		$array = Array('id'=>$id);
		$this->connection->delSql("admin_mail_addr",$array);
	}
	function addAddr($add_name,$add_addr,$addrClass,$domain,$subscripted) {
		$sql="insert into admin_mail_addr(name,mail_addr,domain,class_id,subcripted) values ('".$add_name."','".$add_addr."','".$domain."',".$addrClass.",".$subscripted.")";
		$this->connection->execute($sql);
	}
	function editAddr($id,$name,$addr,$classId,$domain,$subscripted) {
		$sql="update admin_mail_addr set name='".$name."', mail_addr='".$addr."',domain='".$domain."', class_id=".$classId.", subcripted=".$subscripted." where id=".$id;
		
		$this->connection->execute($sql);
	}
}

$connect = new Connect("localhost","root","","email");
$addrOpt = new AddrOpt($connect);
$opt=$_POST['opt'];
if (isset($opt)) {
	if ($opt=='del') {
		$id = $_POST['id'];
		$addrOpt->removeAddr($id);
	}
    if ($opt=='add') {
		$add_name = $_POST['add_name'];
		$add_addr = $_POST['add_addr'];
		$subscripted = $_POST['subscripted'];
		$addrClass = $_POST['addrClass'];
		$domain = substr($add_addr, strpos($add_addr,'@') + 1);
		$addrOpt->addAddr($add_name,$add_addr,$addrClass,$domain,$subscripted);
	}
	if ($opt=='edit') {
		$id = $_POST['id'];
		$name = $_POST['name'];
		$addr = $_POST['addr'];
		$classId = $_POST['classId'];
		$domain = substr($addr, strpos($addr,'@') + 1);
		$subscripted = $_POST['subscripted'];
		
		$addrOpt->editAddr($id,$name,$addr,$classId,$domain,$subscripted);
	}
}

?>